﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace T18BaseOfGraphics//图形界面绘制(窗体，键盘鼠标，计时器，界面，动画)
{
    public partial class FrmMain : Form
    {
        private int x, y;//鼠标当前坐标
        private int timerTickCount = 0;//计时器的次数
        private Bitmap bmAll;//原始位图
        private Bitmap[] bmFragmets = new Bitmap[48];//动画帧位图
        private int displayIndex = 0;//起始帧索引

        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("窗体大小(像素){0},{1}",
                this.Width.ToString(),
                this.Height.ToString()
                );
            sb.AppendLine();
            sb.AppendFormat(
                "工作区域大小(像素){0},{1}",
                this.ClientRectangle.Width.ToString(),
                this.ClientRectangle.Height.ToString()
                );
            MessageBox.Show(sb.ToString(), "信息提示");
            //初始化计时器Timer1
            this.toolStripStatusLabel1.Text = "计时次数:" + timerTickCount.ToString();
            this.timer1.Interval = 1000;//间隔1秒
            this.timer1.Start();

            //初始化动画位图资源:载入大图，然后再分割，来减少10带来的资源消耗
            bmAll = new Bitmap(Application.StartupPath + "\\threerings.bmp");
            for (int row = 0; row < 8; row++)
            {
                for (int col = 0; col < 6; col++)
                {
                    bmFragmets[row * 6 + col] = bmAll.Clone(
                        new Rectangle(col * 75, row * 75, 75, 75),
                        bmAll.PixelFormat);
                }
            }
            //初始化计时器Timer2
            this.timer2.Interval = 80;
            this.timer2.Start();
        }

        private void FrmMain_KeyDown(object sender, KeyEventArgs e)
        {
            Debug.WriteLine(e.KeyCode.ToString());
            //按Esc键关闭窗口
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }

        private void FrmMain_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                MessageBox.Show("单击鼠标左键", "提示");
            }
            else if (e.Button == MouseButtons.Right)
            {
                MessageBox.Show("单击鼠标右键", "提示");
            }
            else if (e.Button == MouseButtons.Middle)
            {
                MessageBox.Show("单击鼠标中键", "提示");
            }
        }

        private void FrmMain_MouseMove(object sender, MouseEventArgs e)
        {
            this.x = e.X;
            this.y = e.Y;
            this.Invalidate();
        }

        private void FrmMain_Paint(object sender, PaintEventArgs e)
        {
            //图形类
            Graphics g = e.Graphics;

            //绘制鼠标坐标信息 -- 文字
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("鼠标当前坐标:{0},{1}", x.ToString(), y.ToString());
            g.DrawString(sb.ToString(),
                new Font("微软雅黑", 20f),
                new SolidBrush(Color.Crimson),
                this.ClientRectangle.Width / 2 + 20, this.ClientRectangle.Height / 2 - 50
                );

            //绘制分割线
            Pen pen = new Pen(Color.Black, 2.0f);
            g.DrawLine(pen,
                this.ClientRectangle.Width / 2, 0,
                this.ClientRectangle.Width / 2, this.ClientRectangle.Height);
            g.DrawLine(pen,
                0, this.ClientRectangle.Height / 2,
                this.ClientRectangle.Width, this.ClientRectangle.Height / 2);

            //绘制图形
            pen.Color = Color.Blue;
            g.DrawRectangle(pen, 2, 2, 300, 240);
            g.FillRectangle(new SolidBrush(Color.LightSkyBlue), 15, 15, 274, 214);
            pen.Color = Color.DeepPink;
            g.DrawBezier(pen,
                new Point(100, 2),
                new Point(10, 10),
                new Point(100, 200),
                new Point(120, 100));

            //绘制文字
            g.DrawString("游戏开始",
                new Font("微软雅黑", 32),
                new SolidBrush(Color.OrangeRed),
                new Point(400, 40));

            //绘制图片
            Bitmap bm = new Bitmap(Application.StartupPath + "\\c.png");//\\.c.png是转义的意思
            g.DrawImage(bm, new Rectangle(2, 290, 360, 240));
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timerTickCount++;
            this.toolStripStatusLabel1.Text = "计时次数:" + timerTickCount.ToString();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            //动画逻辑
            displayIndex = (++displayIndex) % 48;
            if (displayIndex == 47 && this.timer2.Interval >= 20)
            {
                this.timer2.Interval -= 10;
            }
            //刷新界面
            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            //预先在内存中绘制所有内容，在一次性绘制到前端
            Bitmap buff = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
            Graphics g = Graphics.FromImage(buff);
            //绘制到缓冲区（内存）
            g.DrawImage(bmFragmets[displayIndex], new Point(500, 350));
            //绘制到前端
            e.Graphics.DrawImage(buff, 0, 0);
            //释放缓冲区内存
            g.Dispose();
            base.OnPaint(e);
        }
    }
}
